//
//  FKChooseCityVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/18.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKChooseCityVC.h"
#import "FKLocationManager.h"
#import "FKChooseCityCell.h"
#import "FKChooseCitySectionHeader.h"

@interface FKChooseCityVC ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *datas;

@property (nonatomic, strong) NSArray *openCities;
@property (nonatomic, strong) NSDictionary *locationCity;

@end

@implementation FKChooseCityVC

static NSString * const ID = @"FKChooseCityCell";
static NSString *const headerID = @"FKChooseCitySectionHeader";

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat margin = fk_adjustW(15);
    CGFloat itemW = (fkScreenW - 24 - 3 * margin) / 4.f;
    layout.itemSize = CGSizeMake(itemW, fk_adjustW(30));
    layout.headerReferenceSize = CGSizeMake(fkScreenW, fk_adjustW(44));
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;
    layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择城市";
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.alwaysBounceVertical = true;
    
    [self.collectionView registerClass:[FKChooseCityCell class] forCellWithReuseIdentifier:ID];
    [self.collectionView registerClass:[FKChooseCitySectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
    
    [self loadData];
    [[FKLocationManager sharedLocation] getCity:^(NSString *addressString) {
        self.locationCity = @{@"title" : addressString};
        [self dealData];
//        NSLog(@"---(%@", addressString);
//        if ([addressString containsString:@"市"]) {
//            self.locationCity = [addressString stringByReplacingOccurrencesOfString:@"市" withString:@""];
//        }
    }];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.datas.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSDictionary *group = self.datas[section];
    NSArray *items = group[@"items"];
    return items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKChooseCityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    NSDictionary *group = self.datas[indexPath.section];
    NSArray *items = group[@"items"];
    cell.data = items[indexPath.item];
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *group = self.datas[indexPath.section];
    NSArray *items = group[@"items"];
    NSDictionary *cityInfo = items[indexPath.item];
    if (cityInfo[@"id"]) {
        [FKCacheTool saveStoreId:cityInfo[@"id"]];
    }
    if (self.chooseCityBlock) {
        self.chooseCityBlock(cityInfo);
    }
    fkPopVC;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        FKChooseCitySectionHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
        NSDictionary *group = self.datas[indexPath.section];
        header.title = group[@"headerTitle"];
        return header;
    }
    return nil;
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Index&a=store" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            self.openCities = result.data[@"list"];
            [self dealData];
        }
    } failure:nil];
}

- (void)dealData
{
    NSMutableArray *datas = [NSMutableArray array];
    if (self.locationCity) {
        NSMutableDictionary *locationGroup = [NSMutableDictionary dictionary];
        [datas addObject:locationGroup];
        locationGroup[@"headerTitle"] = @"定位城市";
        locationGroup[@"items"] = @[self.locationCity];
    }
    if (self.openCities.count > 0) {
        NSMutableDictionary *serviceGroup = [NSMutableDictionary dictionary];
        [datas addObject:serviceGroup];
        serviceGroup[@"headerTitle"] = @"本地服务开通城市";
        serviceGroup[@"items"] = self.openCities;
    }
    self.datas = datas;
    [self.collectionView reloadData];
}

@end
